﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PetaPoco;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class LogLoginController : Base
    {

        private readonly LogLoginService _logLoginService;
        private readonly LoginUtil _LoginUtil;
        public LogLoginController(LoginUtil LoginUtil, LogLoginService  logLoginService)
        {
            _LoginUtil = LoginUtil;
            _logLoginService = logLoginService;
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Log_Index)]
        public async Task<IActionResult> IndexAsync(int page = 1, int limit = 15, int json = 0)
        {
            if (json == 1)
            {
                int userId = await _LoginUtil.LoginUserId();
                int groupId = _LoginUtil.LoginGroupId();
                int adminUid = groupId != 1 ? userId : 0;//超级管理员显示全部数据
                Page<ViewLogLogin> list = await _logLoginService.GetSqlListAsync(page, limit, adminUid);
                return Json(new { code = 0, data = list.Items, count = list.TotalItems });
            }
            return View();
        }

        [HttpPost]
        [ActionPermissionFilter(Permission = PermissionKeys.Log_Clear)]
        [Log(Name = "清除登录日志")]
        public async Task<IActionResult> Clear()
        {
            await _logLoginService.Clear();
            return Json(new { status = 1, msg = "清除成功！" });
        }
    }
}
